Skip to main content

Compute the roots of a quadratic equation by accepting the coefficients Program and Algorithm Computer Science Engineering VTU

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
    float a,b,c,x1,x2,sp,ip,disc;
    printf("enter the co-efficients\n");
    scanf("%f%f%f",&a,&b,&c);
    if(a==0)
    {
        printf("a cannot be zero");
        exit(0);
    }
    disc=b*b-4*a*c;
    if(disc==0)
    x1=x2=-b/(2*a);
    printf("the roots are equal\n");
    printf("x1=%f\n x2=%f\n",x1,x2);
}
    else if(disc>0)
{
    x1=(-b+sqrt(disc))/(2*a);
    x2=(-b-sqrt(disc))/(2*a);
    printf("the roots are real and distinct\n");
    printf("x1=%f\n x2=%f\n";x1,x2);
}
else 
{
    sp=-b/(2*a);
    ip=sqrt(fabs(disc))/(2*a);
    printf("the roots are Complex\n");
    printf("first root=%f+i%f\n",sp,ip);
    printf("second root=%f-i%f\n",sp,ip);

Algorithm

Start 

Read the co-efficient as a, b, c

if a is equal to 0 display error 

else compute disc=(b*b)-(4*a*c) and r=sqrt(fabs(disc)))

if disc is equal to 0 then 

compute x1=x2= -b/(a*2.0)) and display x1,x2

else If discriminant is greater than 0 then 

compute (x1=(-b+r)/(a*2.0)), x2= (-b-r)/(a*2.0)) and display x1,x2

else If discriminant is lesser than zero, 

display roots are complex 

compute x1=(-b)/(a*2.0)+ir/(a*2.0) and x2= (-b)/(a*2.0)-ir/(a*2.0) and display x1,x2

Stop

Comments

Popular posts from this blog

Raju is a Civil Engineer. He is writing software to automate his work. As a part of his requirement, he wants to calculate the circle diameter, circumference, and area. Help Raju to complete his task. Get radius as input.

OUTPUT

Compute sin(x)/cos(x) using Taylor series approximation.Program and Algorithm Computer Science Engineering VTU

Taylor series  #include<stdio.h> #include<stdlib.h> #include<math.h> int main() {  int ch,i;  float num,den,x,degree,sum=0,term;  printf("enter 1 for sin(x)\n enter 2 for cos(x)\n");  scanf("%d",&ch);  printf("enter the degree\n");  scanf("%f",&degree);  x=(degree*3.14)/180.0;  switch(ch) {   case 1:num=x;   den=1;   i=2; break;   case 2:num=1;   den=1;   i=1;   break;   default:printf("enter the proper choice\n");   exit(0); }   do {   term=num/den;   num=-num*x*x;   den=den*i*(i+1);   sum=sum+term;   i=i+2; } while(fabs(term)>0.000001);   if(ch==1) {   printf("sin(%f)without using built in function=%f\n",degree,sum);   printf("sin(%f)with using built in function=%f\n",degree,sin(x)); } else {  printf("cos(%f) without using buil...